
package com.idealighter.game.shop.manager;

import com.google.inject.Inject;
import com.google.inject.Singleton;

import com.idealighter.game.backpack.manager.BackpackMgr;
import com.idealighter.game.dblog.service.DbLogService;
import com.idealighter.game.dictionary.dic.ShopItemDic;
import com.idealighter.game.dictionary.domain.ShopItemDomain;
import com.idealighter.game.log.core.LogReason;
import com.idealighter.game.log.struct.shop.BuyItemLog;
import com.idealighter.game.player.manager.PlayerMgr;
import com.idealighter.game.player.struct.Player;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * 商城 .
 * 
 * @date 2015年9月2日 下午5:10:38
 *
 */
@Singleton
public class ShopMgr {

  @Inject
  private ShopMsgMgr shopMsgMgr;
  @Inject
  private ShopItemDic shopItemDic;

  private static final Logger LOG = LoggerFactory.getLogger(ShopMgr.class);

  // 元宝货币
  public static final int INGOT = 1;
  // 金币货币
  public static final int GOLD = 2;

  @Inject
  private PlayerMgr playerMgr;
  @Inject
  private BackpackMgr itemMgr;

  /**
   * 购买物品 .
   * 
   * @param player 玩家.
   * @param itemId .
   * @param num .
   * @param moneyType . 货币类型(1:元宝,2:金币),即item类型
   */
  public void buyItem(Player player, int itemId, int num, int moneyType) {
    ShopItemDomain shopItem = shopItemDic.map().get(itemId);

    if (shopItem == null) {
      LOG.error("玩家[{}][{}]购买的道具[{}]不存在", player.getPlayerName(), player.getId(), itemId);
      return;
    }

    if (num > shopItem.getMaxCount()) {
      LOG.error("玩家[{}][{}]购买的道具[{}]数量[{}]超过最大购买数量", player.getPlayerName(), player.getId(), itemId,
          num);
      return;
    }

    int price = 0;
    int totalCost = 0;
    final int totalGiveGold = shopItem.getGive() * num;
    final long beforeIngot = player.getIngot();
    final long beforeGold = player.getGold();

    if (moneyType == INGOT) {
      price = shopItem.getPriceIngot();
      totalCost = price * num;
      if (player.getIngot() < totalCost) {
        LOG.error("玩家[{}][{}]购买的道具[{}]时元宝[{}]不足", player.getPlayerName(), player.getId(), itemId,
            player.getIngot());
        shopMsgMgr.sendBuyItemMsg(player, 1, 0, 0);
        return;
      }
    } else if (moneyType == GOLD) {
      price = shopItem.getPriceGold();
      totalCost = price * num;
      if (player.getGold() < totalCost) {
        LOG.error("玩家[{}][{}]购买的道具[{}]时金币[{}]不足", player.getPlayerName(), player.getId(), itemId,
            player.getGold());
        shopMsgMgr.sendBuyItemMsg(player, 1, 0, 0);
        return;
      }
    } else {
      LOG.warn("玩家[{}][{}]购买道具[{}]时的使用的货币类型[{}]不支持", player.getPlayerName(), player.getId(), itemId,
          moneyType);
      return;
    }

    itemMgr.removeItem(player, moneyType, totalCost, LogReason.BUY_ITEM);
    itemMgr.addItem(player, itemId, num, LogReason.BUY_ITEM);
    // 购买道具赠送游戏币
    if (totalGiveGold > 0) {
      playerMgr.addGold(player, totalGiveGold, LogReason.BUY_ITEM_GIVE);
    }

    shopMsgMgr.sendBuyItemMsg(player, 0, itemId, num);
    DbLogService.log(
        new BuyItemLog(player, shopItem.getName(), num, price, shopItem.getGive(), totalGiveGold,
            moneyType, totalCost, beforeIngot, player.getIngot(), beforeGold, player.getGold()));
    LOG.info("玩家[{}][{}]购买道具[{}]数量[{}]成功", player.getPlayerName(), player.getId(), itemId, num);
  }
}
